﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using ERMap;

namespace PhoneBook.Group
{
    public class PbRight
    {
        public static DataSet list(string phonebookid)
        {
            Hashtable keys = new Hashtable();
            keys["phonebookid"] = phonebookid;

            return ERMapHelper.getdata("b_pbright", keys);
        }

        public static void reset(string phonebookid, string righttype, IList memberids)
        {
            Hashtable keys = new Hashtable();
            keys["phonebookid"] = phonebookid;
            keys["righttype"] = righttype;
            ERMapHelper.destroy("b_pbright", keys);

            foreach (var item in memberids)
            {
                Hashtable pbright = new Hashtable();
                pbright["phonebookid"] = phonebookid;
                pbright["righttype"] = righttype;
                pbright["memberid"] = item.ToString();

                ERMapHelper.setmodel("b_pbright", "phonebookid,righttype,memberid", pbright);
            }
        }

        public static DataSet queryright(string righttype, string memberid)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" select p.*,r.memberid,r.righttype ");
            sb.Append(" from b_phonebook p left outer join b_pbright r");
            sb.Append(" on p.phonebookid = r.phonebookid ");
            sb.Append(" and r.righttype = @righttype and memberid = @memberid ");
            sb.Append(" where isnull(p.isdel,0)= 0 and isnull(r.isdel,0)= 0 ");

            Hashtable keys = new Hashtable();
            keys["righttype"] = righttype;
            keys["memberid"] = memberid;

            return ERMapHelper.getCustomData(sb.ToString(), keys);
        }

        public static DataSet queryright(string righttype, string memberid, string boid)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" select p.*,r.memberid,r.righttype ");
            sb.Append(" from b_phonebook p left outer join b_pbright r");
            sb.Append(" on p.phonebookid = r.phonebookid ");
            sb.Append(" and r.righttype = @righttype and memberid = @memberid ");
            sb.Append(" inner join b_branchoffice b");
            sb.Append(" on b.id = p.boid and b.id = @boid");
            sb.Append(" where isnull(p.isdel,0)= 0 and isnull(r.isdel,0)= 0 ");

            Hashtable keys = new Hashtable();
            keys["righttype"] = righttype;
            keys["memberid"] = memberid;
            keys["boid"] = boid;

            return ERMapHelper.getCustomData(sb.ToString(), keys);
        }

        public static void add(string phonebookid, string righttype, string memberid)
        {
            Hashtable pbshare = new Hashtable();
            pbshare["phonebookid"] = phonebookid;
            pbshare["righttype"] = righttype;
            pbshare["memberid"] = memberid;
            ERMapHelper.setmodel("b_pbright", "phonebookid,righttype,memberid", pbshare);
        }

        public static void del(string phonebookid, string righttype, string memberid)
        {
            Hashtable pbshare = new Hashtable();
            pbshare["phonebookid"] = phonebookid;
            pbshare["righttype"] = righttype;
            pbshare["memberid"] = memberid;
            ERMapHelper.destroy("b_pbright", pbshare);
        }
    }
}
